package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Comment;
import com.blueheart.music.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author gali
 * @date 2022/4/9 15:07
 */
@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 增加评论
     * @param request
     * @return
     */
    @PostMapping("/comment/addComment")
    public ResultUtil addComment(HttpServletRequest request){
        String userId = request.getParameter(MyConst.USER_ID).trim();
        String type = request.getParameter(MyConst.TYPE).trim();
        String songId = request.getParameter(MyConst.SONGID).trim();
        String songListId = request.getParameter(MyConst.SONGLISTID).trim();
        String content = request.getParameter(MyConst.CONTENT).trim();
        if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(type)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Comment comment = new Comment();
        comment.setUserId(new Integer(userId));
        comment.setType(new Integer(type));
        if (new Integer(type)==0){
            comment.setSongId(new Integer(songId));
        }else {
            comment.setSongListId(new Integer(songListId));
        }
        comment.setContent(content);
        boolean flag = commentService.addComment(comment);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @PostMapping("/comment/deleteComment")
    public ResultUtil deleteComment(@RequestParam("id") Integer id){
        if (id>0){
            boolean flag = commentService.removeComment(id);
            if (flag){
                return ResultUtil.success();
            }
            return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 更新评论
     * @param request
     * @return
     */
    @PostMapping("/comment/updateComment")
    public ResultUtil updateComment(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID).trim();
        String userId = request.getParameter(MyConst.USER_ID).trim();
        String type = request.getParameter(MyConst.TYPE).trim();
        String songId = request.getParameter(MyConst.SONGID).trim();
        String songListId = request.getParameter(MyConst.SONGLISTID).trim();
        String content = request.getParameter(MyConst.CONTENT).trim();
        if (StringUtils.isEmpty(userId)||StringUtils.isEmpty(type)||StringUtils.isEmpty(id)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Comment comment = new Comment();
        comment.setId(new Integer(id));
        comment.setUserId(new Integer(userId));
        comment.setType(new Integer(type));
        if (new Integer(type)==0){
            comment.setSongId(new Integer(songId));
        }else {
            comment.setSongListId(new Integer(songListId));
        }
        comment.setContent(content);
        boolean flag = commentService.reviseComment(comment);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }

    /**
     * 查询全部评论
     * @return
     */
    @GetMapping("/comment/findAll")
    public ResultUtil findAll(){
        List<Comment> list = commentService.findAll();
        if (list.size()>0){
            return ResultUtil.success(list);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 查询歌单中评论
     * @param songListId
     * @return
     */
    @PostMapping("/comment/findBySongListId")
    public ResultUtil findBySongListId(@RequestParam("songListId") Integer songListId){
        if (songListId>0){
            List<Comment> bySongListId = commentService.findBySongListId(songListId);
            if (bySongListId.size()>0){
                return ResultUtil.success(bySongListId);
            }
            return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return  ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 查询歌曲评论
     * @param songId
     * @return
     */
    @PostMapping("/comment/findBySongId")
    public ResultUtil findBySongId(@RequestParam("songId")Integer songId){
        if (songId>0){
            List<Comment> bySongId = commentService.findBySongId(songId);
            if (bySongId.size()>0){
                return ResultUtil.success(bySongId);
            }
            return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return  ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 增加点赞数
     * @param request
     * @return
     */
    @PostMapping("/comment/addUp")
    public ResultUtil addUp(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID);
        String up = request.getParameter(MyConst.UP);
        if (StringUtils.isEmpty(id)||StringUtils.isEmpty(up)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        Comment comment = new Comment();
        comment.setId(new Integer(id));
        comment.setUp(new Integer(up));
        boolean flag = commentService.addUp(comment);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }
}
